









<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>testnguice - Introduction</title>
    <link rel="stylesheet" href="./css/maven-theme.css" type="text/css" />
    <link rel="stylesheet" href="./css/maven-base.css" type="text/css"/>
    <link rel="stylesheet" href="./css/prettify.css" type="text/css"/>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
    <script type="text/javascript" src="./js/prettify.js"></script>
    <script type="text/javascript" src="./js/niftycube.js"></script>
    <script type="text/javascript">
        window.onload=function() {
            Nifty("div\#wrapper", "big");
            Nifty("div#breadcrumbs", "small");
            Nifty("div\#navcolumn", "small");
            Nifty("div.source", "medium");
            prettyPrint();
        }
    </script>
          <meta name="author" content="Simone Tripodi" />
          </head>
  <body>
    <div id="wrapper">
        <div id="banner">
                        <div id="bannerLeft">
    
            TestNGuice
    
            </div>
                            <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="breadcrumbs">
              
  

  
    
    
  
    
            <div class="xleft">
        <span id="publishDate">Last Published: 2009-11-24</span>
                  &nbsp;| <span id="projectVersion">Version: 2.0</span>
                      </div>
                <div class="xright">      
  

  
    
    
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="leftColumn">
          <div id="navcolumn">
               
  

  
    
    
  
    
                       <h5>Injectlet</h5>
            <ul>
              
    <li class="none">
                    <a href="index.html">Introduction</a>
          </li>
              
    <li class="none">
              <strong>Injection and TestCases</strong>
        </li>
              
    <li class="none">
                    <a href="providers.html">Data Providers</a>
          </li>
          </ul>
              <h5>Project Documentation</h5>
            <ul>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-info.html">Project Information</a>
                </li>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-reports.html">Project Reports</a>
                </li>
          </ul>
              <h5>External Links</h5>
            <ul>
              
    <li class="none">
                    <a href="../testnguice">Project Page</a>
          </li>
              
    <li class="none">
                    <a href="../testnguice/downloads/list">Downloads</a>
          </li>
              
    <li class="none">
                    <a href="http://testng.org" class="externalLink">TestNG</a>
          </li>
              
    <li class="none">
                    <a href="http://testng.org/doc/book.html" class="externalLink">TEH TestNG Book</a>
          </li>
              
    <li class="none">
                    <a href="../google-guice">Google Guice</a>
          </li>
              
    <li class="none">
                    <a href="http://opencsv.sourceforge.net/" class="externalLink">OpenCSV</a>
          </li>
          </ul>
                                               <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img class="poweredBy"
                 alt="Built by Maven"
                 src="./images/logos/maven-feather.png"
                 width="90"
                 height="30"
                 border="1" />
          </a>
                           
  

  
    
    
  
    
            </div>
        </div>
        <div id="bodyColumn">
          <div id="contentBox">
            <div class="section"><h2>Injection and TestCases</h2>
<p>Very easy - but only if you already know both TestNG and google-guice!!!</p>
<p>Step 1: define your business objects</p>
<div class="source"><pre>interface Service {

    void go();

}

class ServiceImpl implements Service {

    public void go() {
        System.out.println(&quot;This is a test message&quot;);
    }

}</pre>
</div>
<p>Step 2: define now the TestNG Object Factory</p>
<div class="source"><pre>class ServiceTestObjectFactory extends com.testnguice.inject.AbstractGuiceObjectFactory {

    public ServiceTestObjectFactory() {
        super(new Module() {
            public void configure(Binder binder) {
                binder.bind(Service.class).to(ServiceImpl.class);
            }
        });
    }

}</pre>
</div>
<p>Step 3: define your test case(s)</p>
<div class="source"><pre>class ServiceTestCase {

    @com.google.inject.Inject
    private Service service;

    public void setService(Service service) {
        this.service = service;
    }

    @org.testng.annotations.Test
    public void verifyExpectedMessage() {
        assert &quot;This is a test message&quot;.equals(service.getMessage());
    }

}</pre>
</div>
<p>Step 4: configure <tt>testng.xml</tt> in way to specify the Object Factory</p>
<div class="source"><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;&gt;
&lt;suite name=&quot;my-test-suite&quot;&gt;

    &lt;test name=&quot;my-test&quot;&gt;
        &lt;classes&gt;
            &lt;class name=&quot;ServiceTestObjectFactory&quot;/&gt;
            &lt;class name=&quot;ServiceTestCase&quot;/&gt;
        &lt;/classes&gt;
    &lt;/test&gt;

&lt;/suite&gt;</pre>
</div>
<p>Don't forget to specify the Object Factory <b>before</b> the rest of test classes!!!</p>
<p>Final step: Run TestNG and enjoy :)</p>
</div>

          </div>
        </div>
        <div class="clear">
          <hr/>
        </div>
        <div id="footer">
          <div class="xright">Copyright &#169;         
   
          2009.
    
      All Rights Reserved.    
  

  
    
    
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
    </div>
  </body>
</html>
